<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>进车订单管理</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/purchase.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/salesman/salesmanlist.css">
</head>
<body>
<header class="dashboard-header">
    <div class="container">
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <h1 class="h3 mb-0"><i class="bi bi-bicycle"></i> 电动车销售管理系统</h1>
                <p class="mb-0 opacity-75">业务员工作台</p>
            </div>
            <div class="d-flex align-items-center">
                <div class="me-3">
                    <span class="d-none d-md-inline">${currentUser.name}</span>
                    <div class="badge bg-light text-dark">
                        <i class="bi bi-person-circle me-1"></i> 业务员
                    </div>
                </div>
                <div class="dropdown">
                    <button class="btn btn-light rounded-circle p-1" type="button" data-bs-toggle="dropdown">
                        <i class="bi bi-gear fs-5"></i>
                    </button>
                    <ul class="dropdown-menu dropdown-menu-end">
                        <li><a class="dropdown-item" href="${pageContext.request.contextPath}/changePassword"><i class="bi bi-lock me-2"></i> 修改密码</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item text-danger" href="${pageContext.request.contextPath}/LogoutServlet"><i class="bi bi-box-arrow-right me-2"></i> 退出登录</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</header>

<div class="container">
    <!-- 页面标题和操作按钮 -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-0"><i class="bi bi-bicycle me-2"></i>进车订单管理</h2>
            <p class="text-muted mb-0">管理您的车辆采购订单</p>
        </div>
        <div>
            <!-- 添加新订单按钮 -->
            <a href="${pageContext.request.contextPath}/purchase/new" class="btn btn-primary">
                <i class="bi bi-plus-lg me-1"></i> 添加新订单
            </a>
            <a href="${pageContext.request.contextPath}/vehicle/new" class="btn btn-vehicle">
                <i class="bi bi-bicycle me-1"></i> 添加车辆信息
            </a>
        </div>
    </div>

    <!-- 统计卡片 -->
    <div class="row mb-4">
        <div class="col-md-4">
            <a href="${pageContext.request.contextPath}/purchase/list?type=all" class="text-decoration-none">
                <div class="summary-card ${filterType == 'all' ? 'summary-card-active' : ''}">
                    <i class="bi bi-list-check fs-1 text-primary"></i>
                    <div class="number">${totalOrders}</div>
                    <div class="title">总订单数</div>
                </div>
            </a>
        </div>
        <div class="col-md-4">
            <a href="${pageContext.request.contextPath}/purchase/list?type=pending" class="text-decoration-none">
                <div class="summary-card ${filterType == 'pending' ? 'summary-card-active' : ''}">
                    <i class="bi bi-clock-history fs-1 text-warning"></i>
                    <div class="number">${pendingOrders}</div>
                    <div class="title">待审核订单</div>
                </div>
            </a>
        </div>
        <div class="col-md-4">
            <a href="${pageContext.request.contextPath}/purchase/list?type=approved" class="text-decoration-none">
                <div class="summary-card ${filterType == 'approved' ? 'summary-card-active' : ''}">
                    <i class="bi bi-check-circle fs-1 text-success"></i>
                    <div class="number">${approvedOrders}</div>
                    <div class="title">已审核订单</div>
                </div>
            </a>
        </div>
    </div>

    <!-- 错误/成功消息提示 -->
    <c:if test="${not empty errorMessage}">
        <div class="alert alert-danger alert-dismissible fade show mb-4" role="alert">
            <i class="bi bi-exclamation-triangle me-2"></i> ${errorMessage}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        <c:remove var="errorMessage" scope="session"/>
    </c:if>

    <c:if test="${not empty successMessage}">
        <div class="alert alert-success alert-dismissible fade show mb-4" role="alert">
            <i class="bi bi-check-circle me-2"></i> ${successMessage}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        <c:remove var="successMessage" scope="session"/>
    </c:if>

    <!-- 订单列表 -->
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <span>${pageTitle}</span>
            <div class="d-flex">
                <div class="search-bar me-2">
                    <i class="bi bi-search text-muted me-2"></i>
                    <input type="text" class="border-0 bg-transparent" placeholder="搜索订单..." id="searchInput">
                </div>
                <div class="dropdown">
                    <button class="btn btn-sm btn-light dropdown-toggle" type="button" data-bs-toggle="dropdown">
                        <i class="bi bi-filter me-1"></i> 筛选
                    </button>
                    <ul class="dropdown-menu dropdown-menu-end">
                        <li><a class="dropdown-item" href="${pageContext.request.contextPath}/purchase/list">全部订单</a></li>
                        <li><a class="dropdown-item" href="${pageContext.request.contextPath}/purchase/list?type=pending">待审核</a></li>
                        <li><a class="dropdown-item" href="${pageContext.request.contextPath}/purchase/list?type=approved">已审核</a></li>
                        <li><hr class="dropdown-divider"></li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="table-light">
                    <tr>
                        <th>订单ID</th>
                        <th>车辆信息</th>
                        <th>数量</th>
                        <th>总价</th>
                        <th>状态</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <c:forEach items="${orders}" var="order">
                        <tr class="order-item fade-in">
                            <td><strong>#${order.id}</strong></td>
                            <td>
                                <div class="d-flex align-items-center">
                                    <c:choose>
                                        <c:when test="${not empty order.vehicle.pic}">
                                            <img src="${pageContext.request.contextPath}/images/vehicles/${order.vehicle.pic}"
                                                 class="vehicle-img me-3" alt="${order.vehicle.brand}">
                                        </c:when>
                                        <c:otherwise>
                                            <div class="vehicle-img me-3 default-img">
                                                <i class="bi bi-image"></i>
                                            </div>
                                        </c:otherwise>
                                    </c:choose>
                                    <div>
                                        <c:choose>
                                            <c:when test="${not empty order.vehicle}">
                                                <div class="fw-medium">${order.vehicle.brand} ${order.vehicle.model}</div>
                                                <div class="text-muted small">${order.vehicle.color} · ${order.vehicle.productionYear}</div>
                                            </c:when>
                                            <c:otherwise>
                                                <div class="fw-medium text-danger">车辆信息缺失</div>
                                                <div class="text-muted small">ID: ${order.v_id}</div>
                                            </c:otherwise>
                                        </c:choose>
                                    </div>
                                </div>
                            </td>
                            <td>${order.quantity} 辆</td>
                            <td class="fw-bold">
                                <c:if test="${not empty order.vehicle}">
                                    ¥${order.quantity * order.vehicle.price}
                                </c:if>
                                <c:if test="${empty order.vehicle}">
                                    -
                                </c:if>
                            </td>
                            <td>
                                <c:choose>
                                    <c:when test="${order.status == 0}">
                                        <span class="status-badge status-pending">待审核</span>
                                    </c:when>
                                    <c:otherwise>
                                        <span class="status-badge status-approved">已审核</span>
                                    </c:otherwise>
                                </c:choose>
                            </td>
                            <td><span class="text-muted">${order.createTime}</span></td>
                            <td>
                                <div class="d-flex gap-2">
                                    <a href="${pageContext.request.contextPath}/purchase/view?id=${order.id}"
                                       class="btn btn-sm btn-outline-primary action-btn">
                                        <i class="bi bi-eye"></i> 查看
                                    </a>
                                    <c:if test="${order.status == 0}">
                                        <a href="${pageContext.request.contextPath}/purchase/edit?id=${order.id}"
                                           class="btn btn-sm btn-outline-warning action-btn">
                                            <i class="bi bi-pencil"></i> 编辑
                                        </a>
                                        <a href="${pageContext.request.contextPath}/purchase/delete?id=${order.id}"
                                           class="btn btn-sm btn-outline-danger action-btn delete-btn"
                                           onclick="return confirm('确定要删除订单 #${order.id} 吗？')">
                                            <i class="bi bi-trash"></i> 删除
                                        </a>
                                    </c:if>
                                </div>
                            </td>
                        </tr>
                    </c:forEach>
                    </tbody>
                </table>
            </div>

            <c:if test="${empty orders}">
                <div class="text-center py-5">
                    <i class="bi bi-inbox fs-1 text-muted"></i>
                    <h5 class="mt-3">暂无进车订单</h5>
                    <p class="text-muted">您还没有创建任何进车订单，点击"添加新订单"开始创建</p>
                    <a href="${pageContext.request.contextPath}/purchase/new" class="btn btn-primary mt-2">
                        <i class="bi bi-plus-lg me-1"></i> 添加新订单
                    </a>
                    <!-- 在无订单时也显示添加车辆信息按钮 -->
                    <a href="${pageContext.request.contextPath}/vehicle/new" class="btn btn-vehicle mt-2 ms-2">
                        <i class="bi bi-bicycle me-1"></i> 添加车辆信息
                    </a>
                </div>
            </c:if>
        </div>

        <!-- 分页 -->
        <div class="card-footer d-flex justify-content-between align-items-center">
            <div class="text-muted small">显示 1 到 ${fn:length(orders)} 项，共 ${totalOrders} 项</div>
            <nav>
                <ul class="pagination pagination-sm mb-0">
                    <li class="page-item disabled"><a class="page-link" href="#">上一页</a></li>
                    <li class="page-item active"><a class="page-link" href="#">1</a></li>
                    <li class="page-item"><a class="page-link" href="#">2</a></li>
                    <li class="page-item"><a class="page-link" href="#">3</a></li>
                    <li class="page-item"><a class="page-link" href="#">下一页</a></li>
                </ul>
            </nav>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
    // 搜索功能
    document.getElementById('searchInput').addEventListener('input', function(e) {
        const searchTerm = e.target.value.toLowerCase();
        document.querySelectorAll('.order-item').forEach(row => {
            const rowText = row.textContent.toLowerCase();
            row.style.display = rowText.includes(searchTerm) ? '' : 'none';
        });
    });

    document.querySelectorAll('.delete-btn').forEach(btn => {
        btn.addEventListener('click', function(e) {
            if (!confirm('确定要删除这个订单吗？操作不可恢复！')) {
                e.preventDefault();
            }
        });
    });

    document.querySelectorAll('.alert .btn-close').forEach(button => {
        button.addEventListener('click', function() {
            this.closest('.alert').style.display = 'none';
        });
    });

    // 消息自动消失
    setTimeout(() => {
        const messages = document.querySelectorAll('.message');
        messages.forEach(msg => {
            msg.style.opacity = '0';
            setTimeout(() => msg.style.display = 'none', 300);
        });

        // 发送 AJAX 请求，通知服务器清空消息
        fetch('${pageContext.request.contextPath}/ClearMessageServlet', {
            method: 'GET',
            headers: {
                'X-Requested-With': 'XMLHttpRequest'
            }
        });
    }, 3000);
</script>
</body>
</html>